﻿namespace Beetle
{
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Security.Cryptography;

    public class TcpUtils
    {
        internal static bool _field_a = false;
        internal static Class_ag _field_b;
        internal static object _field_c = new object();
        internal static object _field_d = new object();
        internal static Class_h _field_e;
        private static IList<Class_ah> _field_f = new List<Class_ah>();
        private static IList<Class_ah> _field_g = new List<Class_ah>();
        private static IList<Class_ah> _field_h = new List<Class_ah>();
        internal static object _field_i = new object();
        internal static object _field_j = new object();
        internal static int _field_k = 1;
        private static int _field_l = 0;
        private static int _field_m = 0;
        private static int _field_n = 0;
        public static int BufferPoolSize = 0x3e8;
        public static ObjectPool<BufferReader> BufferReaderPool;
        public static IByteConverterFactory ByteConverters = new Class_z();
        public static int ChannelMaxQueueData = 0;
        public static int Connections = 0x3e8;
        public static int DataPacketMaxLength = 0x200c;
        public static ChannelController DefaultController = new ChannelController();
        public static bool ExecutionContext = false;
        public static int ReceiveBufferLength = 0x800;
        public static long ReceiveBytes;
        public static long ReceiveDataIO;
        public static long ReceiveMessages;
        public static int SendBufferLength = 0x800;
        public static long SendBytes;
        public static long SendDataIO;
        public static long SendMessages;
        public static int SendMessageSpacing = 0;
        public static int SendPacketSpacing = 0;
        public static int SocketDespatchSleepTime = 10;
        public static bool Statistics = false;
        public static int StringEncodingLength = 0x200;
        public static int TimeOutms = 0x7530;
        public static bool UseReceiveAsyncQueue = true;
        public static bool UseStringEncoding = true;
        public static int WorkDespatchSleepTime = 10;

        static TcpUtils()
        {
            f();
        }

        internal static object a()
        {
            return new RSACryptoServiceProvider(0x800);
        }

        private static void a(int A_0)
        {
            Class_ag.d();
            Class_ag.a(A_0, BufferPoolSize);
        }

        internal static Class_ah b()
        {
            lock (_field_h)
            {
                if ((_field_h == null) || (_field_h.Count > 0))
                {
                    NetTcpException.NotInitialize();
                }
                _field_n++;
                if (_field_n == _field_h.Count)
                {
                    _field_n = 0;
                }
                return _field_h[_field_n];
            }
        }

        internal static Class_ah c()
        {
            lock (_field_g)
            {
                _field_m++;
                if (_field_m >= _field_g.Count)
                {
                    _field_m = 0;
                }
                return _field_g[_field_m];
            }
        }

        public static void Clean()
        {
            if (BufferReaderPool != null)
            {
                BufferReaderPool.Dispose();
                BufferReaderPool = null;
            }
            if (_field_e != null)
            {
                _field_e.Dispose();
            }
            if (ByteConverters != null)
            {
                ByteConverters.Clear();
            }
            e();
            if (_field_b != null)
            {
                _field_b.Dispose();
                _field_b = null;
            }
            _field_g.Clear();
            _field_f.Clear();
            _field_h.Clear();
        }

        internal static Class_ah d()
        {
            lock (_field_f)
            {
                _field_l++;
                if (_field_l >= _field_f.Count)
                {
                    _field_l = 0;
                }
                return _field_f[_field_l];
            }
        }

        private static void e()
        {
            foreach (Class_ah _ah in _field_f)
            {
                _ah.Dispose();
            }
            foreach (Class_ah _ah2 in _field_g)
            {
                _ah2.Dispose();
            }
            foreach (Class_ah _ah3 in _field_h)
            {
                _ah3.Dispose();
            }
        }

        private static void f()
        {
        }

        internal static string g()
        {
            return AppDomain.CurrentDomain.BaseDirectory;
        }

        public static int GetAsyncEventPoolStatus()
        {
            return _field_e.b();
        }

        public static IList<int> GetBufferPoolStatus()
        {
            return Class_ag.c();
        }

        public static IList<int> GetReceiveDespatchStatus()
        {
            List<int> list = new List<int>();
            foreach (Class_ah _ah in _field_g)
            {
                list.Add(_ah.d());
            }
            return list;
        }

        public static int GetSendBufferStatus()
        {
            if (_field_b == null)
            {
                return 0;
            }
            return _field_b.f();
        }

        public static IList<int> GetSendDespatchStatus()
        {
            List<int> list = new List<int>();
            foreach (Class_ah _ah in _field_f)
            {
                list.Add(_ah.d());
            }
            return list;
        }

        public static IList<int> GetWorkDespatchsStatus()
        {
            IList<int> list = new List<int>();
            if (_field_h != null)
            {
                for (int i = 0; i < _field_h.Count; i++)
                {
                    list.Add(_field_h[i].d());
                }
            }
            return list;
        }

        public static void Setup(int maxonlineconnections)
        {
            Setup(maxonlineconnections, 1, 1);
        }

        public static void Setup(string configname)
        {
            ConfigSection section = (ConfigSection) ConfigurationManager.GetSection(configname);
            if (section == null)
            {
                throw NetTcpException.ConfigSelectionNotFound(configname);
            }
            Statistics = section.Statistics;
            SocketDespatchSleepTime = section.SocketThreadSleep;
            WorkDespatchSleepTime = section.WorkThreadSleep;
            StringEncodingLength = section.StringEncodingSize;
            ReceiveBufferLength = section.ReceiveBufferSize;
            SendBufferLength = section.SendBufferSize;
            DataPacketMaxLength = section.PackageMaxSize;
            TimeOutms = section.TimeOut;
            ChannelMaxQueueData = section.ChannelMaxQueueData;
            ExecutionContext = section.ExecutionContext;
            Setup(section.Connections, section.Pools, section.SocketSendThreads, section.SocketReceiveThreads, section.WorkThreads);
        }

        public static void Setup(int maxonlineconnections, int despatchs)
        {
            Setup(maxonlineconnections, Convert.ToInt32(Math.Ceiling((double) (((double) maxonlineconnections) / 1000.0))) * 2, despatchs);
        }

        public static void Setup(int maxconnections, int bufferpools, int despatchs)
        {
            Setup(maxconnections, bufferpools, despatchs, despatchs, despatchs);
        }

        public static void Setup(int maxconnections, int bufferpools, int senddespatchs, int receivedespatch, int workdespatchs)
        {
            Class_ah _ah;
            Clean();
            Connections = maxconnections;
            ByteConverters.Init(Connections);
            _field_e = new Class_h(Connections, ReceiveBufferLength);
            BufferReaderPool = new ObjectPool<BufferReader>(Connections / 2);
            a(bufferpools);
            _field_b = new Class_ag(Connections / 2, SendBufferLength, true);
            for (int i = 0; i < senddespatchs; i++)
            {
                _ah = new Class_ah {
                    _field_c = SocketDespatchSleepTime
                };
                _field_f.Add(_ah);
            }
            for (int j = 0; j < receivedespatch; j++)
            {
                _ah = new Class_ah {
                    _field_c = SocketDespatchSleepTime
                };
                _field_g.Add(_ah);
            }
            for (int k = 0; k < workdespatchs; k++)
            {
                _ah = new Class_ah {
                    _field_c = WorkDespatchSleepTime
                };
                _field_h.Add(_ah);
            }
            _field_a = true;
        }
    }
}

